import logging
from init import PATHS
LOGGER = logging.getLogger(__name__)
import numpy as np
import pandas as pd
from C_PatentVariables import conventions_names_colors, firm_year_aggregation, b_patent_oems
from E_Analysis import reading_datasets
dict_subsector_shortnames, dict_var_colors, list_of_subsectors_in_cleancars = conventions_names_colors.main()
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('seaborn')
import seaborn as sns


def spillovers_suppliers_to_OEMS():
    # To what extent do OEMs cite the patents of their own suppliers?
    dfcitations = get_citations_made_by_oems_in_batfamilies()
    dfcitations = add_variables_coding_for_whether_the_citation_goes_to_a_supplier(dfcitations)
    dfcitations = add_variables_coding_for_selfcitation(dfcitations)
    output_stackplot_total(dfcitations)
    output_stackplot_average(dfcitations)
    hybrids_spillovers()
    return


def get_citations_made_by_oems_in_batfamilies():
    # First, get all the docdbids of families filed by OEMs (with year)
    df_bvdid_docdbid = firm_year_aggregation.get_docdbids_of_bvdids('OEMs')
    # add oem ids
    cols = ['Year', 'OEM_Level1_ID', 'Level1_bvdid', 'OEM_Level2_ID', 'Level2_bvdid', 'Sub_BvDID']
    OEMs = pd.read_csv(PATHS.dropbox / 'Data_outputted/A_AutoIndustry/OEM_and_Subsidiaries.csv', usecols=cols).drop_duplicates()
    df_agg_corres = b_patent_oems.get_docdbids_to_aggregate(OEMs, df_bvdid_docdbid, aggregation_level='OEM_Level1_ID', with_subsi=True)
    df_agg_corres = df_agg_corres.drop(columns=['earliest_filing_year', 'bvdid', 'Year'])
    df_agg_corres = df_agg_corres.rename(columns={'docdb_family_id': 'citing_docdb_family_id'})
    # df_agg_corres is simply a df with OEM_Level1_ID, citing_docdb_family_id [we'll add years later]
    dfcitations = add_citationsinfo(df_agg_corres)
    return dfcitations



def add_citationsinfo(df_agg_corres):
    # Get all citation info
    # We've already cleaned up citations info for battery families so easier to focus on these families rather than all the families of oems.
    dfcitations = pd.read_csv(PATHS.dropbox / 'Data_outputted/C_PatentVariables/Citations_backward_of_AllBatFC_Families.csv')
    dfcitations['citing_year'] = dfcitations['citing_appln_filing_date'].apply(lambda x: int(x[:4]) if type(x) == str else x)
    mask_hascitations = dfcitations['cited_docdb_family_id'].notnull()
    maskyear = dfcitations['citing_year'].isin(range(1990, 2018))
    maskBatorFC = dfcitations[f'Sub-sector_citing'].str.contains('batteries')
    dfcitations = dfcitations[maskBatorFC & mask_hascitations & maskyear]
    citationscols = ['citing_docdb_family_id', 'citing_year', 'cited_docdb_family_id', 'bvdid', 'NAICScore3digit',
                     '336', '335', '334', '333', '541', 'OEMs', 'subsidiaries', 'suppliers', 'nace_motor',
                     'ipc_cpc_transpo']
    dfcitations = dfcitations[citationscols].drop_duplicates()
    dfcitations = df_agg_corres.merge(dfcitations, on='citing_docdb_family_id', how='inner')
    return dfcitations


def add_variables_coding_for_whether_the_citation_goes_to_a_supplier(dfcitations):
    # Identify citations going to own suppliers
    df_supplier_docdbid = firm_year_aggregation.get_docdbids_of_bvdids('suppliers')
    df_supplierid = pd.read_csv(PATHS.dropbox / 'Data_outputted/B_FactsetVariables/suppliers_ids.csv')
    df_supplierid = df_supplierid[['fctid', 'bvdid']].drop_duplicates()
    df_supplier_docdbid = df_supplierid.merge(df_supplier_docdbid, on='bvdid', how='inner')
    df_supplier_docdbid = df_supplier_docdbid.rename(columns={'docdb_family_id': 'cited_docdb_family_id',
                                                              'fctid': 'cited_fctid', 'bvdid': 'cited_bvdid'})
    # Combine OEM citations and bvdid of cited
    data = dfcitations[['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id']].drop_duplicates()
    data = data.merge(df_supplier_docdbid[['cited_docdb_family_id', 'cited_bvdid', 'cited_fctid']].drop_duplicates(), on='cited_docdb_family_id', how='left')
    data = data[data['cited_fctid'].notnull()]
    # now we need to figure out the status of OEM_Level1_ID - cited_bvdid/cited_fctid in citing_year
    # Import info about supplier-oems relationship
    dflinks = pd.read_csv(PATHS.dropbox / 'Data_outputted/B_FactsetVariables/SC_ProdSuppPanel.csv')
    cols = ['OEM_Level1_ID', 'SUPPLIER_ID', 'Year', 'Years_since_firstactive']
    data = data.merge(dflinks[cols].drop_duplicates(), left_on=['OEM_Level1_ID', 'cited_fctid', 'citing_year'],
                      right_on=['OEM_Level1_ID', 'SUPPLIER_ID', 'Year'], how='left')
    first = dflinks[dflinks['Active']][['SUPPLIER_ID', 'Year']].groupby('SUPPLIER_ID').min()['Year'].rename('Years_since_firstactive_allOEMs')
    data = data.merge(first, on='SUPPLIER_ID', how='left')
    data['Estab_Own'] = (data['Years_since_firstactive'] >= 0)
    data['Futur_Own'] = (data['Years_since_firstactive'] < 0)
    data['Estab_Other'] = (data['Years_since_firstactive_allOEMs'] >= data['citing_year'])
    data['Futur_Other'] = (data['Years_since_firstactive_allOEMs'] < data['citing_year'])
    cols = ['Estab_Own', 'Futur_Own', 'Estab_Other', 'Futur_Other']
    data = data.groupby(['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id'])[cols].agg(lambda x: True if True in x.unique() else False)
    # Merging back into full citations info
    dfcitations = dfcitations.merge(data, on=['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id'], how='outer')
    dfcitations = dfcitations.fillna(False)
    return dfcitations



def add_variables_coding_for_selfcitation(dfcitations):
    # First, get all the docdbids of families filed by OEMs (with year)
    df_bvdid_docdbid = firm_year_aggregation.get_docdbids_of_bvdids('OEMs')
    # add oem ids
    cols = ['Year', 'OEM_Level1_ID', 'Level1_bvdid', 'OEM_Level2_ID', 'Level2_bvdid', 'Sub_BvDID']
    OEMs = pd.read_csv(PATHS.dropbox / 'Data_outputted/A_AutoIndustry/OEM_and_Subsidiaries.csv', usecols=cols).drop_duplicates()
    df_agg_corres = b_patent_oems.get_docdbids_to_aggregate(OEMs, df_bvdid_docdbid, aggregation_level='OEM_Level1_ID', with_subsi=True)
    df_agg_corres = df_agg_corres.drop(columns=['earliest_filing_year', 'bvdid', 'Year'])
    df_agg_corres = df_agg_corres.rename(columns={'docdb_family_id': 'cited_docdb_family_id',
                                                  'OEM_Level1_ID': 'OEM_Level1_ID_cited'}).drop_duplicates()
    data = dfcitations[['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id']].drop_duplicates()
    data = data.merge(df_agg_corres, on='cited_docdb_family_id', how='left')
    data['SelfCitation'] = (data['OEM_Level1_ID'] == data['OEM_Level1_ID_cited'])
    data['OtherOEM'] = (~data['SelfCitation']) & (data['OEM_Level1_ID_cited'].notnull())
    cols = ['SelfCitation', 'OtherOEM']
    data = data.groupby(['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id'])[cols].agg(lambda x: True if True in x.unique() else False)
    # Merging back into full citations info
    dfcitations = dfcitations.merge(data, on=['OEM_Level1_ID', 'citing_year', 'cited_docdb_family_id'], how='outer')
    dfcitations['SelfCitation'] = dfcitations['SelfCitation'].fillna(False)
    dfcitations['OtherOEM'] = dfcitations['OtherOEM'].fillna(False)
    return dfcitations



def output_stackplot_total(dfcitations):
    a0 = dfcitations.groupby('citing_year')['cited_docdb_family_id'].count().rename('TotNbrCitations')
    mask1 = dfcitations['SelfCitation']
    a1 = dfcitations[mask1].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_self')
    mask2 = (~dfcitations['SelfCitation']) & dfcitations['Estab_Own']
    a2 = dfcitations[mask2].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_ownestablishedsupp')
    mask3 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'])) & dfcitations['Futur_Own']
    a3 = dfcitations[mask3].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_ownfuturesupp')
    a = a0.reset_index().merge(a1, on='citing_year', how='outer').merge(a2, on='citing_year', how='outer').merge(a3, on='citing_year', how='outer')
    mask4 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'])) & dfcitations['suppliers']
    a4 = dfcitations[mask4].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_othersuppliers')
    a = a.merge(a4, on='citing_year', how='outer')
    mask5 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'])) & (dfcitations['334'] | dfcitations['335'])
    a5 = dfcitations[mask5].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_electronics')
    a = a.merge(a5, on='citing_year', how='outer')
    mask6 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'] | dfcitations['334'] | dfcitations['335'])) & dfcitations['OtherOEM']
    a6 = dfcitations[mask6].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_otherOEMs')
    a = a.merge(a6, on='citing_year', how='outer')
    mask7 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'] | dfcitations['334'] | dfcitations['335'] | dfcitations['OtherOEM'])) & dfcitations['nace_motor']
    a7 = dfcitations[mask7].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_nacemotor')
    a = a.merge(a7, on='citing_year', how='outer')
    a = a.fillna(0)
    a['subtot'] = (a['NbrCit_self'] + a['NbrCit_ownestablishedsupp'] + a['NbrCit_ownfuturesupp']
                   + a['NbrCit_othersuppliers'] + a['NbrCit_electronics'] + a['NbrCit_otherOEMs']
                   + a['NbrCit_nacemotor'] )
    a['NbrCit_undefined'] = a['TotNbrCitations'] - a['subtot']
    a = a[a['citing_year'] <= 2015]
    a = a[a['citing_year'] > 2004]
    # PLOT FIGURE
    sns.set(style="white")
    pal2 = sns.color_palette('Blues')
    # sns.palplot(pal2)
    plt.stackplot(a['citing_year'],
                  a['NbrCit_self'],
                  a['NbrCit_ownestablishedsupp'],
                  a['NbrCit_ownfuturesupp'],
                  a['NbrCit_othersuppliers'],
                  a['NbrCit_electronics'],
                  a['NbrCit_otherOEMs'],
                  a['NbrCit_nacemotor'],
                  a['NbrCit_undefined'],
                  labels=['Self', 'Established Supplier', 'Future Supplier', 'Suppliers of Others', 'Electronics',
                          'Other Carmakers', 'Other NACE Motor', 'Other'],
                  # colors=[pal2[-6], pal2[-4], pal2[-2]], linewidth=0
                  )
    plt.ylabel('Number of Citations')
    plt.legend(loc='upper left', ncol=1)
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited.png', bbox_inches='tight')
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited.pdf', bbox_inches='tight')
    plt.close()
    return




def output_stackplot_average(dfcitations):
    a0 = dfcitations.groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('TotNbrCitations')
    mask1 = dfcitations['SelfCitation']
    a1 = dfcitations[mask1].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_self')
    mask2 = (~dfcitations['SelfCitation']) & dfcitations['Estab_Own']
    a2 = dfcitations[mask2].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_ownestablishedsupp')
    mask3 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'])) & dfcitations['Futur_Own']
    a3 = dfcitations[mask3].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_ownfuturesupp')
    a = a0.reset_index().merge(a1, on=['citing_year', 'OEM_Level1_ID'], how='outer').merge(a2, on=['citing_year', 'OEM_Level1_ID'], how='outer').merge(a3, on=['citing_year', 'OEM_Level1_ID'], how='outer')
    mask4 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'])) & dfcitations['suppliers']
    a4 = dfcitations[mask4].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_othersuppliers')
    a = a.merge(a4, on=['citing_year', 'OEM_Level1_ID'], how='outer')
    mask5 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'])) & (dfcitations['334'] | dfcitations['335'])
    a5 = dfcitations[mask5].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_electronics')
    a = a.merge(a5, on=['citing_year', 'OEM_Level1_ID'], how='outer')
    mask6 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'] | dfcitations['334'] | dfcitations['335'])) & dfcitations['OtherOEM']
    a6 = dfcitations[mask6].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_otherOEMs')
    a = a.merge(a6, on=['citing_year', 'OEM_Level1_ID'], how='outer')
    mask7 = (~(dfcitations['SelfCitation'] | dfcitations['Estab_Own'] | dfcitations['Futur_Own'] | dfcitations['suppliers'] | dfcitations['334'] | dfcitations['335'] | dfcitations['OtherOEM'])) & dfcitations['nace_motor']
    a7 = dfcitations[mask7].groupby(['citing_year', 'OEM_Level1_ID'])['cited_docdb_family_id'].count().rename('NbrCit_nacemotor')
    a = a.merge(a7, on=['citing_year', 'OEM_Level1_ID'], how='outer')
    a = a.fillna(0)
    a['subtot'] = (a['NbrCit_self'] + a['NbrCit_ownestablishedsupp'] + a['NbrCit_ownfuturesupp']
                   + a['NbrCit_othersuppliers'] + a['NbrCit_electronics'] + a['NbrCit_otherOEMs']
                   + a['NbrCit_nacemotor'] )
    a['NbrCit_undefined'] = a['TotNbrCitations'] - a['subtot']
    a = a[a['citing_year'] <= 2015]
    a = a[a['citing_year'] > 2004]
    # AVERAGE CARMAKER OVER THE YEARS
    a = a.groupby('citing_year').mean().reset_index()
    # PLOT FIGURE
    sns.set(style="white")
    pal2 = sns.color_palette('Blues')
    # sns.palplot(pal2)
    plt.stackplot(a['citing_year'], a['NbrCit_self'], a['NbrCit_ownestablishedsupp'],
                  a['NbrCit_ownfuturesupp'], a['NbrCit_othersuppliers'],
                  a['NbrCit_electronics'], a['NbrCit_otherOEMs'],
                  a['NbrCit_nacemotor'], a['NbrCit_undefined'],
                  labels=['Self', 'Established Suppliers', 'Future Suppliers', 'Suppliers of Others', 'Electronics',
                          'Other Carmakers', 'Other Motor Vehicle', 'Others'],
                  # colors=[pal2[-6], pal2[-4], pal2[-2]], linewidth=0
                  )
    plt.ylabel('Number of Citations')
    plt.legend(loc='upper left', bbox_to_anchor=(1, 1), ncol=1)
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited_averagecarmaker.png', bbox_inches='tight')
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited_averagecarmaker.pdf', bbox_inches='tight')
    plt.close()
    a['NbrCit_self'] = a['NbrCit_self'] / a['TotNbrCitations']
    a['NbrCit_ownestablishedsupp'] = a['NbrCit_ownestablishedsupp'] / a['TotNbrCitations']
    a['NbrCit_ownfuturesupp'] = a['NbrCit_ownfuturesupp'] / a['TotNbrCitations']
    a['NbrCit_othersuppliers'] = a['NbrCit_othersuppliers'] / a['TotNbrCitations']
    a['NbrCit_electronics'] = a['NbrCit_electronics'] / a['TotNbrCitations']
    a['NbrCit_otherOEMs'] = a['NbrCit_otherOEMs'] / a['TotNbrCitations']
    a['NbrCit_nacemotor'] = a['NbrCit_nacemotor'] / a['TotNbrCitations']
    a['NbrCit_undefined'] = a['NbrCit_undefined'] / a['TotNbrCitations']
    a['TotNbrCitations'] = a['TotNbrCitations'] / a['TotNbrCitations']
    # PLOT FIGURE
    sns.set(style="white")
    pal2 = sns.color_palette('Blues')
    # sns.palplot(pal2)
    plt.stackplot(a['citing_year'], a['NbrCit_self'], a['NbrCit_ownestablishedsupp'],
                  a['NbrCit_ownfuturesupp'], a['NbrCit_othersuppliers'],
                  a['NbrCit_electronics'], a['NbrCit_otherOEMs'],
                  a['NbrCit_nacemotor'], a['NbrCit_undefined'],
                  labels=['Self', 'Established Suppliers', 'Future Suppliers', 'Suppliers of Others', 'Electronics',
                          'Other Carmakers', 'Other Motor Vehicle', 'Others'],
                  # colors=[pal2[-6], pal2[-4], pal2[-2]], linewidth=0
                  )
    plt.ylabel('Share of Citations')
    plt.legend(loc='upper left', bbox_to_anchor=(1, 1), ncol=1)
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited_averagecarmaker_share.png', bbox_inches='tight')
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'stackplot_OEM_bat_citations_bytypeoffirmcited_averagecarmaker_share.pdf', bbox_inches='tight')
    plt.close()
    b = a.mean()
    return




def hybrids_spillovers():
    dfcitations = pd.read_csv(PATHS.dropbox / 'Data_outputted/C_PatentVariables/Citations_backward_of_AllBatFC_Families.csv')
    dfcitations['citing_year'] = dfcitations['citing_appln_filing_date'].apply(lambda x: int(x[:4]) if type(x) == str else x)
    mask_hascitations = dfcitations['cited_docdb_family_id'].notnull()
    maskyear = dfcitations['citing_year'].isin(range(1990, 2018))
    dfcitations = dfcitations[mask_hascitations & maskyear]

    mask = dfcitations['Sub-sector'].notnull() & dfcitations['Sub-sector'].str.contains('hybrid vehicles') & ~(
            dfcitations['Sub-sector'].str.contains('batteries') | dfcitations['Sub-sector'].str.contains('fuel cells'))
    dfcitations['Hybrid'] = False
    dfcitations.loc[dfcitations[mask].index, 'Hybrid'] = True
    dfcitations['Hybrid'].value_counts()

    maskCitingHybrid = dfcitations['Hybrid']
    maskBat = dfcitations['Sub-sector_citing'].str.contains('batteries')
    a0 = dfcitations[maskBat].groupby('citing_year')['cited_docdb_family_id'].count().rename('TotNbrCitations_Bat')
    a1 = dfcitations[maskBat & maskCitingHybrid].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_Hybrid_Bat')
    aBat = a0.reset_index().merge(a1, on='citing_year', how='outer')

    maskFC = dfcitations['Sub-sector_citing'].str.contains('fuel cells')
    a0 = dfcitations[maskFC].groupby('citing_year')['cited_docdb_family_id'].count().rename('TotNbrCitations_FC')
    a1 = dfcitations[maskFC & maskCitingHybrid].groupby('citing_year')['cited_docdb_family_id'].count().rename('NbrCit_Hybrid_FC')
    aFC = a0.reset_index().merge(a1, on='citing_year', how='outer')
    a = aBat.merge(aFC, on='citing_year', how='outer')
    a['ShareCit_Hybrid_Bat'] = a['NbrCit_Hybrid_Bat'] / a['TotNbrCitations_Bat']
    a['ShareCit_Hybrid_FC'] = a['NbrCit_Hybrid_FC'] / a['TotNbrCitations_FC']
    a['Cit_Hybrid_Bat_per1000'] = 1000 * a['NbrCit_Hybrid_Bat'] / a['TotNbrCitations_Bat']
    a['Cit_Hybrid_FC_per1000'] = 1000 * a['NbrCit_Hybrid_FC'] / a['TotNbrCitations_FC']
    a = a[a['citing_year'].isin(range(1990, 2015))]

    dfhybrids = pd.read_csv(PATHS.dropbox / 'Data_outputted/C_PatentVariables/Families_of_ipc_cpc_transpo_cpc_ipc.csv')
    maskhybrid = dfhybrids['Sub-sector'].str.contains('hybrid vehicles')
    dfhybrids = dfhybrids[maskhybrid][['docdb_family_id', 'Sub-sector']].drop_duplicates()
    maskBatFC = dfhybrids['Sub-sector'].str.contains('batteries') | dfhybrids['Sub-sector'].str.contains('fuel cells')
    dfhybrids = dfhybrids[~maskBatFC]
    # get year
    dfhybridsyear = pd.read_csv(PATHS.dropbox / 'Data_outputted/C_PatentVariables/Families_of_ipc_cpc_transpo.csv', usecols=['docdb_family_id', 'appln_filing_date'])
    dfhybridsyear['Year'] = dfhybridsyear['appln_filing_date'].apply(lambda x: int(x[:4]) if type(x) == str else x)
    dfhybrids = dfhybrids.merge(dfhybridsyear, on='docdb_family_id', how='left')
    dfhybrids = dfhybrids[dfhybrids['Year'].isin(range(1990, 2015))]
    dfhybridscounts = dfhybrids.groupby('Year')['docdb_family_id'].count().rename('hybridcount')

    a = a.merge(dfhybridscounts, left_on='citing_year', right_on='Year')


    fig, ax1 = plt.subplots()
    line1, = ax1.plot(a['citing_year'], a['Cit_Hybrid_Bat_per1000'], label='Battery Citations to Hybrid', color=dict_var_colors['Count_Bat'][1], linewidth=3)
    line2, = ax1.plot(a['citing_year'], a['Cit_Hybrid_FC_per1000'], label='Fuel Cell Citations to Hybrid', color=dict_var_colors['Count_FC'][1], linewidth=3)
    ax1.set_xlabel('Year')
    ax1.set_ylabel('Number of Citations to Hybrid (per 1000)', color='black')  # You can adjust the color if needed
    ax1.set_ylim(0, 1.25*a['Cit_Hybrid_Bat_per1000'].max())
    # Create a second y-axis for the hybrid count
    ax2 = ax1.twinx()
    line3, = ax2.plot(a['citing_year'], a['hybridcount'], label='Nbr of Hybrid Families', color='grey', linewidth=2, linestyle='--')
    ax2.set_ylabel('Number of Hybrid Families', color='grey')  # Set the label for the second y-axis
    ax2.set_ylim(0, 1.1*a['hybridcount'].max())
    # Manually create the legend handles and labels
    handles = [line3, line1, line2]
    labels = [h.get_label() for h in handles]
    fig.legend(handles, labels, loc='lower left', bbox_to_anchor=(0.12, 0.72), frameon=True)
    # Save the figure
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'hybrid_spillovers.png', bbox_inches='tight')
    plt.savefig(PATHS.figures / 'supplier_spillovers' / 'hybrid_spillovers.pdf', bbox_inches='tight')
    plt.close()

    fig, ax1 = plt.subplots()
    ax1.plot(a['citing_year'], a['Cit_Hybrid_Bat_per1000'], label='Battery', color=dict_var_colors['Count_Bat'][1], linewidth=3)
    ax1.plot(a['citing_year'], a['Cit_Hybrid_FC_per1000'], label='Fuel Cell', color=dict_var_colors['Count_FC'][1], linewidth=3)
    plt.legend(ncol=1)
    plt.xlabel('Year')
    plt.ylabel('Number of Citations to Hybrid (per 1000)')
    plt.savefig(PATHS.figures / 'mainfigures' / 'patenting_total_OEM_Bat_v_FC.png', bbox_inches='tight')
    plt.savefig(PATHS.figures / 'mainfigures' / 'patenting_total_OEM_Bat_v_FC.pdf', bbox_inches='tight')
    plt.close()

    return



